#include "GLLayer.h"
#include "Runtime/Debug/Logger.h"
#ifdef BATTLEFIRE_WIN_PLAYER

void CheckLastOpenGLError(const char* prefix, const char* file, long line, const char* operation)
{
#if BATTLEFIRE_OGL_RENDERER
    GLenum glerr;
    while ((glerr = glGetError()) != GL_NO_ERROR)
    {
        switch (glerr)
        {
        case GL_INVALID_VALUE:
            Debug("%s GL_INVALID_VALUE", operation);
            break;
        default:
            Debug("gl error  0x%x", (int)glerr);
            break;
        }
#if _DEBUG && !ALICE_IPHONE
        std::string str = file;
        const int kMaxErrors = 10;
        int counter = 0;
        int pos = str.find_last_of('\\');
        printf("%s:%ld :", str.substr(pos + 1, str.length() - pos).c_str(), line);
        if (prefix)
        {
            std::string errorString = prefix;
            errorString += ": ";
            const char* gluMsg = reinterpret_cast<const char*>(gluErrorString(glerr));
            if (gluMsg)
            {
                Error("prefix error  %s", gluMsg);

            }
            else
            {
                Error("prefix error : unkown error 0x%x", glerr);
            }
        }
        else
        {
            const char* gluMsg = reinterpret_cast<const char*>(gluErrorString(glerr));
            if (gluMsg)
            {
                Error("%s %s", operation, gluMsg);
            }
            else
            {
                Error("%s 0x%x", operation, glerr);
            }
        }
        ++counter;
        if (counter > kMaxErrors)
        {
            Debug("GL: error count exceeds %i, stop reporting errors\n", kMaxErrors);
            return;
        }
#endif
    }
#endif
}
#endif